package jgl.vajra.management.part.mine.activity;

import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;

import java.util.Map;

import io.reactivex.Observable;
import jgl.vajra.management.R;
import jgl.vajra.management.bean.CustomerRegistrationBean;
import jgl.vajra.management.databinding.ActivityCustomerRegistrationSearchBinding;
import jgl.vajra.management.manager.AccountManager;
import jgl.vajra.management.part.mine.viewModel.CustomerRegistrationSearchViewModel;
import jgl.vajra.management.widget.bean.BaseListData;
import jgl.vajra.management.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.management.widget.interfaces.RefreshRecyclerLoadStatusListener;
import jgl.vajra.management.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.management.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.management.widget.recyclerView.RefreshRecyclerNetConfig;
import jgl.vajra.management.widget.utils.UIUtils;

@CreateViewModel(CustomerRegistrationSearchViewModel.class)
public class CustomerRegistrationSearchActivity extends BaseMVVMActivity<CustomerRegistrationSearchViewModel, ActivityCustomerRegistrationSearchBinding> {
    private String whereEnd;

    @Override
    public int getLayoutId() {
        return R.layout.activity_customer_registration_search;
    }

    @Override
    public void initView() {
        super.initView();
        int internal = getIntent().getIntExtra("internal", 0);

        mBinding.toolbar.setNavigationIcon(R.drawable.ic_left_arrow);
        mBinding.toolbar.setNavigationOnClickListener(view -> finish());


        mBinding.etText.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
                if (keyCode == keyEvent.KEYCODE_ENTER) {
                    String etText = mBinding.etText.getText().toString().trim();
                    if (!TextUtils.isEmpty(etText)) {
                        whereEnd = etText;
                        mBinding.recyclerView.firstLoad();
                    }

                }

                return false;
            }
        });

        mBinding.recyclerView.setRefreshRecyclerNetConfig(new RefreshRecyclerNetConfig() {
            @Override
            public Observable getNetObservable(Map<String, Object> map, int action) {
                map.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
                map.put("internal", internal);
                map.put("whereEnd", whereEnd);
                map.put("pageSize", 30);
                return mViewModel.getCustomerRegistrationList(map);
            }
        });


        SingleTypeBindingAdapter adapter = new SingleTypeBindingAdapter(this, null, R.layout.item_customer_registration);
        mBinding.recyclerView.setAdapter(adapter);
        mBinding.recyclerView.setIsDataObject(true);
        mBinding.recyclerView.setRefreshLoaderMore(false, false);
        mBinding.recyclerView.setRefreshRecyclerLoadStatusListener(new RefreshRecyclerLoadStatusListener<BaseListData<CustomerRegistrationBean>>() {
            @Override
            public void onSucceed(BaseListData<CustomerRegistrationBean> data, int action) {
                mBinding.recyclerView.setRefreshLoaderMore(true, true);

                mBinding.recyclerView.requestNetObjectSuccess(data.data, action);
            }

            @Override
            public void onError(String errorInfo, int errorCode, int action) {
                super.onError(errorInfo, errorCode, action);
            }
        });
//        mBinding.recyclerView.firstLoad();
    }
}
